//
//  VRDeviceDataCell.m
//  VRFirefighting
//
//  Created by Veer on 2018/8/14.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "VRDeviceDataCell.h"

#import "VRDeviceDataModel.h"

@interface VRDeviceDataCell()

@property (nonatomic, assign) UIView *bottomLineView;
@property (nonatomic, weak) UILabel *numberLbl;
@property (nonatomic, weak) UILabel *titleLbl;
@property (nonatomic, weak) UILabel *signalLbl;

@property (nonatomic, weak) UIImageView *statusImgView;
@property (nonatomic, weak) UILabel *statusLbl;


@end

@implementation VRDeviceDataCell

+ (instancetype)cellWith:(UITableView *)tablevie
{
    static NSString *cellID = @"VRDeviceDataCell";
    
    VRDeviceDataCell *cell = [tablevie dequeueReusableCellWithIdentifier:cellID];
    
    if (nil == cell) {
        
        cell = [[VRDeviceDataCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryType = UITableViewCellAccessoryNone;
        // lieview
        [self setupSubView];
        
    }
    return self;
}


- (void)setupSubView
{
    
    UIView *selectedBgView = [[UIView alloc] init];
    selectedBgView.backgroundColor = RGB(238, 238, 238);
    self.selectedBackgroundView = selectedBgView;
    
    UILabel *numberLbl = [[UILabel alloc] init];
    numberLbl.text = @"数据列表";
    numberLbl.textColor = RGB(51, 51, 51);
    numberLbl.font = VRFont(14);
    numberLbl.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:numberLbl];
    self.numberLbl = numberLbl;
    
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.numberOfLines = 0;
    titleLbl.textColor = RGB(51, 51, 51);
    titleLbl.font = VRFont(14);
    titleLbl.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:titleLbl];
    self.titleLbl = titleLbl;
    
    UILabel *signalLbl = [[UILabel alloc] init];
    signalLbl.textColor = RGB(255, 255, 255);
    signalLbl.font = VRFont(14);
    signalLbl.textAlignment = NSTextAlignmentRight;
    signalLbl.layer.cornerRadius = 2;
    signalLbl.layer.masksToBounds = YES;
    signalLbl.backgroundColor = [UIColor redColor];
    signalLbl.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:signalLbl];
    self.signalLbl = signalLbl;
    
    UIImageView *statusImgView = [[UIImageView alloc] init];
    statusImgView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:statusImgView];
    self.statusImgView = statusImgView;
    
    UILabel *statusLbl = [[UILabel alloc] init];
    statusLbl.text = @"数据列表";
    statusLbl.textColor = RGB(140, 140, 140);
    statusLbl.font = VRFont(14);
    statusLbl.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:statusLbl];
    self.statusLbl = statusLbl;
    
    UIView *bottomLineView = [[UIView alloc] init];
    bottomLineView.backgroundColor = RGB(228, 228, 228);
    [self.contentView addSubview:bottomLineView];
    self.bottomLineView = bottomLineView;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat itemW = kScreenWidth * 0.25;
    
    [self.numberLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView.mas_left).offset(itemW * 0.5);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(itemW);
    }];
    
    [self.titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView.mas_left).offset(itemW * 1.5);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(itemW);
    }];
    
    [self.signalLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView.mas_left).offset(itemW * 2.5);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(48);
        make.height.mas_equalTo(20);
    }];
    
    [self.statusLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView.mas_right).offset(-itemW * 0.5+4);
        make.centerY.mas_equalTo(self.contentView);
        make.height.mas_equalTo(20);
    }];
    
    [self.statusImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.statusLbl.mas_left).offset(0);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(16);
        make.height.mas_equalTo(20);
    }];
    
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.contentView);
        make.height.mas_equalTo(0.5);
    }];
}


#pragma mark - setter && getter
- (void)setModel:(VRDeviceDataModel *)model
{
    _model = model;
    
    self.numberLbl.text = [NSString stringWithFormat:@"%03zd", model.number];
    self.titleLbl.text = model.fFacilityModelID;
    
    NSDictionary *typeDict = @{@(0) : @{@"name": @"正常", @"color": RGB(78, 173, 57), @"icon": @"i_data_success"}, @(1) : @{@"name": @"报警", @"color": RGB(238, 11, 25), @"icon": @"i_data_error"}, @(3) : @{@"name": @"故障", @"color": RGB(251, 86, 31), @"icon": @"i_data_error"}};
    self.signalLbl.backgroundColor = typeDict[@(model.fState)][@"color"];
    self.signalLbl.text = typeDict[@(model.fState)][@"name"];
    
    self.statusLbl.text = typeDict[@(model.fState)][@"name"];
    self.statusLbl.textColor = typeDict[@(model.fState)][@"color"];
    
    self.statusImgView.image = [UIImage imageNamed:typeDict[@(model.fState)][@"icon"]];
    
    if (model.fFacilitySwitch == 1) {
        self.contentView.backgroundColor = RGB(140, 140, 140);
        self.titleLbl.textColor = [UIColor whiteColor];
        self.signalLbl.hidden = YES;
        
        self.statusLbl.text = @"关闭";
        self.statusLbl.textColor = [UIColor whiteColor];
        self.statusImgView.hidden = YES;
    }else{
        self.contentView.backgroundColor = RGB(255, 255, 255);
        self.titleLbl.textColor = RGB(51, 51, 51);
        self.signalLbl.hidden = NO;
        self.statusImgView.hidden = NO;
    }
}

@end
